from re import T
from django.db import models
from django.utils.timezone import now
# Create your models here.
from rest_framework.exceptions import ValidationError
import re

# 部门表
class Department(models.Model):
    depName = models.CharField(verbose_name='部门名称', max_length=32)
    statu = models.BooleanField(default=True, verbose_name='状态')
    depTime = models.DateField(default=now, blank=True, null=True,verbose_name='创建部门时间')
    depSort=models.IntegerField(verbose_name='菜单排序')
    depCharge=models.CharField(verbose_name='负责人',max_length=32)
    depStatus=models.BooleanField(default=True,verbose_name='部门状态')
    phone=models.CharField(verbose_name='电话',max_length=11)
    depDesic=models.CharField(verbose_name='备注',max_length=100)
    did = models.ForeignKey(to='self', default=None, null=True,verbose_name='父级id',
                            related_name='children', on_delete=models.CASCADE)
    def __str__(self):
        return self.depName
    def get_children(self):
        return self.children.filter()
    # class Meta:
    #     db_table='department'

# 岗位表
class Post(models.Model):
    postName=models.CharField(max_length=32,verbose_name='岗位名称')
    postStatus=models.BooleanField(default=True,verbose_name='岗位状态')
    postTime=models.DateField(default=now, blank=True, null=True,verbose_name='创建时间')
    postDesic=models.CharField(verbose_name='备注',max_length=100)
    postSort=models.IntegerField(verbose_name='排序')
    postCode=models.CharField(verbose_name='岗位编码',max_length=32,default='')


# 权限表
class Permission(models.Model):
    Type_choices = (
        (0, '目录'),
        (1, '菜单'),
        (2, '按钮')
    )
    perName = models.CharField(verbose_name='权限名称', max_length=32)
    perIndeify = models.CharField(verbose_name='权限标识', max_length=32)
    perTime = models.DateTimeField(
        verbose_name='创建菜单时间', default=now, blank=True, null=True)
    perType = models.IntegerField(choices=Type_choices, verbose_name='菜单类型')
    perSort = models.IntegerField(verbose_name='菜单排序')
    perIcon = models.CharField(verbose_name='菜单图标', max_length=32, blank=True, null=True)
    component = models.CharField(
        verbose_name='组件名称', max_length=32, blank=True, null=True)
    path = models.CharField(verbose_name='组件路径',
                            max_length=32, blank=True, null=True)
    isLink = models.BooleanField(
        default=False, verbose_name='是否外链接', blank=True, null=True)
    isShow = models.BooleanField(
        default=True, verbose_name='是否显示', blank=True, null=True)
    isSave = models.BooleanField(
        default=True, verbose_name='是否缓存', blank=True, null=True)
    mid = models.ForeignKey(to='self', default=None, null=True,
                            related_name='children', on_delete=models.CASCADE)
    # 状态

# 角色表
class Role(models.Model):
    depName = models.CharField(verbose_name='角色名称', max_length=32)
    identify = models.CharField(verbose_name='权限字符', max_length=32)
    roleSort = models.IntegerField(verbose_name='显示顺序/排序')
    roleStatus = models.BooleanField(default=True, verbose_name='角色状态')
    roleTime = models.DateTimeField(
        verbose_name='创建角色时间', default=now, blank=True, null=True)
    roleDesic = models.CharField(verbose_name='备注', max_length=100)
    permission = models.ManyToManyField(to=Permission,blank=True,null=True)



# 左侧菜单表
class Menu(models.Model):
    name = models.CharField(verbose_name='路径名称', max_length=32, default='')
    path = models.CharField(verbose_name='权限路径', max_length=32,blank=True,null=True)
    component = models.CharField(verbose_name='组件名称', max_length=32, null=True)
    mid = models.ForeignKey(to='self', on_delete=models.CASCADE, default=None,blank=True, null=True, related_name='children')
    # metalist = models.ForeignKey(Metalist, on_delete=models.CASCADE, default=None,related_name='meta')
    icon = models.CharField(max_length=32, verbose_name='图标')
    title = models.CharField(verbose_name='权限名称', max_length=32)
    type = models.CharField(default='menu', max_length=32)
    affix=models.BooleanField(default=False,verbose_name='是否固定到tab标签')
    hidden=models.BooleanField(default=False,verbose_name='是否隐藏侧边路由')
    # role=models.ManyToManyField(to=Role)


# 用户表
class User(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='用户编码')
    Gender_choices = (
        (0, '女'),
        (1, '男'),
        (2, '未知')
    )
    user = models.CharField(verbose_name='用户名称', max_length=32)
    sex = models.IntegerField(choices=Gender_choices, verbose_name='性别')
    nickName = models.CharField(verbose_name='用户昵称', max_length=32)
    # id=models.IntegerField(verbose_name='用户编码',primary_key=True)
    password = models.CharField(verbose_name='密码', max_length=100)
    phone = models.CharField(verbose_name='手机号', max_length=11)
    userStatus = models.BooleanField(default=True, verbose_name='用户状态')
    userTime = models.DateTimeField(
        verbose_name='创建时间', default=now, blank=True, null=True)
    email = models.EmailField(verbose_name='邮箱')
    userDesc = models.CharField(verbose_name='备注', max_length=32)
    headPicture = models.URLField(verbose_name='头像', blank=True, null=True)
    depart = models.ForeignKey(
        to=Department, on_delete=models.DO_NOTHING, null=True, related_name='depart')
    post = models.ForeignKey(
        to=Post, on_delete=models.DO_NOTHING, null=True, related_name='post')
    role = models.ManyToManyField(to=Role)
    

    def clean(self):
        if re.match(r'1[3-9]\d{9}$', self.phone) is None:
            raise ValidationError({'phone': ('手机号不合法')})
